﻿using System;
using System.Web.Security;
using System.Web.Configuration;
using DMS.Business.Modules.Configuation;
using DMS.Business.Utility;

namespace DMS.Business.Modules.Organization
{
    /// <summary>
    /// 可验证用户
    /// </summary>
    [Serializable]
    public abstract class AuthenticationUser : Actor
    {
        #region 数据成员/内部成员
        protected string _LoginID;
        protected string _PasswordCode;
        protected ActorStatus _Status = ActorStatus.Actived;
        protected string _Mobile;
        protected string _Email;
        protected bool _IsTemporary = false;
        #endregion

        #region 成员属性
        public override ActorTypes ActorType
        {
            get;
            set;
        }
        /// <summary>
        /// 登录帐号
        /// </summary>
        public virtual string LoginID
        {
            get
            {
                return this._LoginID;
            }
            set
            {
                this._LoginID = value;
            }
        }
        /// <summary>
        /// 用户密码（密文）
        /// </summary>
        public string PasswordCode
        {
            get
            {
                return this._PasswordCode;
            }
            set
            {
                this._PasswordCode = value;
            }
        }
        /// <summary>
        /// 移动电话
        /// </summary>
        public string Mobile
        {
            get
            {
                return this._Mobile;
            }
            set
            {
                this._Mobile = value;
            }
        }
        /// <summary>
        /// 电子邮件
        /// </summary>
        public string Email
        {
            get
            {
                return this._Email;
            }
            set
            {
                this._Email = value;
            }
        }
        /// <summary>
        /// 是否临时帐号
        /// </summary>
        public virtual bool IsTemporary
        {
            get
            {
                return this._IsTemporary;
            }
            set
            {
                this._IsTemporary = value;
            }
        }
        /// <summary>
        /// 用户状态
        /// </summary>
        public virtual ActorStatus Status
        {
            get
            {
                return this._Status;
            }
            set
            {
                this._Status = value;
            }
        }
        private bool? _IsReceiveSMSAlert = null;
        public bool IsReceiveSMSAlert
        {
            get
            {
                if (this._IsReceiveSMSAlert == null)
                {
                    MemberConfig memberConfig = ConfigHelper.GetMemberConfig(this.ID, AppParams.ConfigID_IsReceiveSMSAlert);
                    if (memberConfig == null)
                    {
                        SysConfig sysConfig = ConfigHelper.GetSysConfigByID(AppParams.ConfigID_IsReceiveSMSAlert);
                        this._IsReceiveSMSAlert = sysConfig.Value == "1";
                    }
                    else
                    {
                        this._IsReceiveSMSAlert = memberConfig.Value == "1";
                    }
                }

                return this._IsReceiveSMSAlert == true;
            }
        }

        private bool? _IsReceiveMailAlert = null;
        public bool IsReceiveMailAlert
        {
            get
            {
                if (this._IsReceiveMailAlert == null)
                {
                    MemberConfig memberConfig = ConfigHelper.GetMemberConfig(this.ID, AppParams.ConfigID_IsReceiveMailAlert);
                    if (memberConfig == null)
                    {
                        SysConfig sysConfig = ConfigHelper.GetSysConfigByID(AppParams.ConfigID_IsReceiveMailAlert);
                        this._IsReceiveMailAlert = sysConfig.Value == "1";
                    }
                    else
                    {
                        this._IsReceiveMailAlert = memberConfig.Value == "1";
                    }
                }

                return this._IsReceiveMailAlert == true;
            }
        }

        private string _AlertBeginTime = null;
        public TimeSpan AlertBeginTime
        {
            get
            {
                if (_AlertBeginTime == null)
                {
                    MemberConfig memberConfig = ConfigHelper.GetMemberConfig(this.ID, AppParams.ConfigID_AlertBeginTimeSetting);
                    if (memberConfig == null)
                    {
                        SysConfig sysConfig = ConfigHelper.GetSysConfigByID(AppParams.ConfigID_AlertBeginTimeSetting);
                        this._AlertBeginTime = sysConfig.Value;
                    }
                    else
                    {
                        this._AlertBeginTime = memberConfig.Value;
                    }
                }

                string[] timeInfo = this._AlertBeginTime.Split(":".ToCharArray());
                int hour = int.Parse(timeInfo[0]);
                int minute = int.Parse(timeInfo[1]);
                return new TimeSpan(hour, minute, 0);
            }
        }

        private string _AlertEndTime = null;
        public TimeSpan AlertEndTime
        {
            get
            {
                if (_AlertEndTime == null)
                {
                    MemberConfig memberConfig = ConfigHelper.GetMemberConfig(this.ID, AppParams.ConfigID_AlertEndTimeSetting);
                    if (memberConfig == null)
                    {
                        SysConfig sysConfig = ConfigHelper.GetSysConfigByID(AppParams.ConfigID_AlertEndTimeSetting);
                        this._AlertEndTime = sysConfig.Value;
                    }
                    else
                    {
                        this._AlertEndTime = memberConfig.Value;
                    }
                }

                string[] timeInfo = this._AlertEndTime.Split(":".ToCharArray());
                int hour = int.Parse(timeInfo[0]);
                int minute = int.Parse(timeInfo[1]);
                return new TimeSpan(hour, minute, 0);
            }
        }

        private AuthenticationUser _DelegateUser = null;
        /// <summary>
        /// 委托人
        /// </summary>
        public AuthenticationUser DelegateUser
        {
            get
            {
                return _DelegateUser;
            }
            set
            {
                this._DelegateUser = value;
            }
        }

        private string _DelegateBeginTime = null;
        public string DelegateBeginTime
        {
            get
            {
                if (_DelegateBeginTime == null)
                {
                    MemberConfig memberConfig = ConfigHelper.GetMemberConfig(this.ID, AppParams.ConfigID_DeleBeginTime);
                    if (memberConfig == null)
                    {
                        SysConfig sysConfig = ConfigHelper.GetSysConfigByID(AppParams.ConfigID_DeleBeginTime);
                        this._DelegateBeginTime = sysConfig.Value;
                    }
                    else
                    {
                        this._DelegateBeginTime = memberConfig.Value;
                    }
                }

                return this._DelegateBeginTime;
            }
        }

        private string _DelegateEndTime = null;
        public string DelegateEndTime
        {
            get
            {
                if (_DelegateEndTime == null)
                {
                    MemberConfig memberConfig = ConfigHelper.GetMemberConfig(this.ID, AppParams.ConfigID_DeleEndTime);
                    if (memberConfig == null)
                    {
                        SysConfig sysConfig = ConfigHelper.GetSysConfigByID(AppParams.ConfigID_DeleEndTime);
                        this._DelegateEndTime = sysConfig.Value;
                    }
                    else
                    {
                        this._DelegateEndTime = memberConfig.Value;
                    }
                }

                return this._DelegateEndTime;
            }
        }


        #endregion

        #region 构造函数
        public AuthenticationUser()
        {
        }
        public AuthenticationUser(string id)
            : base(id)
        {
        }
        #endregion

        #region 成员方法
        public abstract void Save();
        public virtual void ResetPassword(string newPassword)
        {
            this._PasswordCode = this.EncryptPWD(newPassword);
            this.Save();
        }
        public virtual bool CheckPassword(string enterPassword)
        {
            return this._PasswordCode == this.EncryptPWD(enterPassword);
        }

        public virtual string EncryptPWD(string pwdStr)
        {
            string pwdCode = string.Empty;
            if (string.IsNullOrEmpty(pwdStr))
            {
                throw new ArgumentNullException("pwdStr", "加密密码时，密码明文不能为空");
            }

            pwdCode = FormsAuthentication.HashPasswordForStoringInConfigFile(pwdStr, FormsAuthPasswordFormat.MD5.ToString());

            return pwdCode;
        }
        #endregion
    }
}
